package com.uduew.sjd.view.xpop;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.uduew.sjd.R;
import com.uduew.sjd.api.HandlingChargeApi;
import com.lxj.xpopup.core.CenterPopupView;

import java.util.ArrayList;

public class DetailsXpop extends CenterPopupView {
    TextView detailsConfirmTv, totalAmountTv, netAmountTv, interestTv, aprTv, repaymentDateTv;
    private String tubulure; //应还金额
    private String strabismal; //利息
    private String telegraphoscope; //实际到账金额
    private ArrayList<HandlingChargeApi.QualitativeBean> qualitative;

    private String ichneumon;

    LinearLayout detailsContentLL;

    public DetailsXpop(@NonNull Context context, String tubulure, String strabismal, String telegraphoscope, ArrayList<HandlingChargeApi.QualitativeBean> qualitative, String ichneumon) {
        super(context);
        this.tubulure = tubulure;
        this.strabismal = strabismal;
        this.telegraphoscope = telegraphoscope;
        this.qualitative = qualitative;
        this.ichneumon = ichneumon;

    }

    private OnAgreeClickListener onAgreeClickListener;

    public void setOnAgreeClickListener(OnAgreeClickListener listener) {
        this.onAgreeClickListener = listener;
    }

    public interface OnAgreeClickListener {
        void onAgreeClick();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.xpop_details;
    }

    private static class ViewHolder {
        TextView tvCustomTitle;
        TextView tvCustomValue;

        ViewHolder(View view) {
            tvCustomTitle = view.findViewById(R.id.tv_custom_title);
            tvCustomValue = view.findViewById(R.id.tv_custom_value);
        }
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        detailsContentLL = findViewById(R.id.details_content_ll);
        Context context = getContext(); // 单次获取上下文
        if (qualitative != null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            int layoutRes = R.layout.layout_customization;
            for (HandlingChargeApi.QualitativeBean qualitativeBean : qualitative) {
                View view = inflater.inflate(layoutRes, detailsContentLL, false); // 正确传递父容器
                ViewHolder holder = new ViewHolder(view);
                holder.tvCustomTitle.setText(
                        !TextUtils.isEmpty(qualitativeBean.getCpcu()) ? qualitativeBean.getCpcu() : ""
                );
                holder.tvCustomValue.setText(
                        !TextUtils.isEmpty(qualitativeBean.getSuperempirical()) ? qualitativeBean.getSuperempirical() : ""
                );
                detailsContentLL.addView(view);
            }
        }

        detailsConfirmTv = findViewById(R.id.details_confirm_tv);
        totalAmountTv = findViewById(R.id.total_amount_tv);
        netAmountTv = findViewById(R.id.net_amount_tv);
        interestTv = findViewById(R.id.interest_tv);
        aprTv = findViewById(R.id.apr_tv);
        repaymentDateTv = findViewById(R.id.repayment_date_tv);
        detailsConfirmTv.setOnClickListener(v -> {
            if (onAgreeClickListener != null) {
                onAgreeClickListener.onAgreeClick();
            }
            dismiss();
        });

        totalAmountTv.setText(tubulure);
        netAmountTv.setText(telegraphoscope);
        interestTv.setText(strabismal);
//        aprTv.setText(cpcu);
        repaymentDateTv.setText(ichneumon);
    }


}